package com.paohe.lianxin_pda;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.paohe.lianxin_pda.utils.common.ConfigUtils;
import com.paohe.lianxin_pda.utils.port.PackageRetrofit;
import com.paohe.lianxin_pda.utils.port.VisitPort;
import com.paohe.lianxin_pda.utils.toast.CustomToast;
import com.zyao89.view.zloading.ZLoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.zyao89.view.zloading.Z_TYPE.CIRCLE;

public class MainActivity extends Activity {

    private Button btnLogin,btnReset;
    private EditText account,pwd;
    private TextView version;
    private String accountVal,passwordVal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLogin = findViewById(R.id.btLogin);
        btnReset = findViewById(R.id.btnReset);
        account = findViewById(R.id.login_account);
        pwd = findViewById(R.id.login_password);
        version = findViewById(R.id.main_txt_version);

        version.setText(ConfigUtils.VERSION);

        //登录按钮
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                accountVal = account.getText().toString().trim();
                passwordVal = pwd.getText().toString().trim();
                if("".equals(accountVal)){
                    CustomToast.showToast(MainActivity.this,"请输入账号！");
                    return;
                }
                if("".equals(passwordVal)){
                    CustomToast.showToast(MainActivity.this,"请输入密码！");
                    return;
                }

                //插件中的Loading样式
                ConfigUtils.DIALOG = new ZLoadingDialog(MainActivity.this);
                ConfigUtils.DIALOG.setLoadingBuilder(CIRCLE)//设置类型
                        .setLoadingColor(Color.BLACK)//颜色
                        .setHintText("Loading...")
                        .show();
                //调用登录接口
                VisitPort vLogin = PackageRetrofit.httpRequest();
                //设置接口参数
                Map map = new HashMap();
                map.put("username",accountVal);
                map.put("password",passwordVal);
                //传递参数
                Call<ResponseBody> callLogin = vLogin.login(map);
                callLogin.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                        try {
                            //获取数据
                            String result = response.body().string();
                            JSONObject jsonObj = new JSONObject(result);
                            String retCode = jsonObj.get("retcode").toString();
                            String retMsg = jsonObj.get("retmsg").toString();
                            if("1".equals(retCode)){
                                ConfigUtils.USERINFO = jsonObj.getJSONObject("user");
                                ConfigUtils.PHONE = jsonObj.getJSONObject("user").get("mobile").toString();

                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH:mm");
                                ConfigUtils.LOGIN_TIME = sdf.format(new Date(Long.parseLong(String.valueOf(jsonObj.getJSONObject("user").get("createTime").toString()))));

                                Intent intent = new Intent(MainActivity.this,IndexActivity.class);
                                startActivity(intent);
                            }else{
                                ConfigUtils.DIALOG.cancel();
                                CustomToast.showToast(MainActivity.this,retMsg+"");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
//                            ConfigUtils.DIALOG.cancel();
                            CustomToast.showToast(MainActivity.this,"登录异常！");
                        } catch (JSONException e) {
                            e.printStackTrace();
//                            ConfigUtils.DIALOG.cancel();
                            CustomToast.showToast(MainActivity.this,"登录异常！");
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        ConfigUtils.DIALOG.cancel();
                    }
                });



            }
        });

        //重置按钮
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,IndexActivity.class);
                startActivity(intent);
            }
        });

   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(ConfigUtils.DIALOG != null){
            ConfigUtils.DIALOG.dismiss();
        }

    }
}
